package com.gigi.datageniebackendmaster.common;

import cn.hutool.core.util.StrUtil;
import com.gigi.datageniebackendmaster.model.enums.ErrorCodeEnum;
import com.gigi.datageniebackendmaster.util.ThrowUtil;
import com.yupi.yucongming.dev.client.YuCongMingClient;
import com.yupi.yucongming.dev.common.BaseResponse;
import com.yupi.yucongming.dev.model.DevChatRequest;
import com.yupi.yucongming.dev.model.DevChatResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 用于对接 AI 平台
 *
 * @author Gigi
 */
@Service
public class AiManager {

    @Resource
    private YuCongMingClient yuCongMingClient;

    /**
     * AI 对话
     *
     * @param modelId
     * @param message
     * @return
     */
    public String doChat(long modelId, String message) {
        DevChatRequest devChatRequest = new DevChatRequest();
        devChatRequest.setModelId(modelId);
        devChatRequest.setMessage(message);
        BaseResponse<DevChatResponse> response = yuCongMingClient.doChat(devChatRequest);
        ThrowUtil.throwIf((Objects.isNull(response) || Objects.isNull(response.getData())),
                ErrorCodeEnum.OPERATION_ERROR, StrUtil.format("AI 响应错误，{}", response.getMessage()));
        return response.getData().getContent();
    }

}
